package controler;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JList;

import utils.Date;
import utils.Dom;
import utils.Time;

import org.jdom.Element;
import org.jdom.Document;

import java.util.Observable;

import template.AbstractListener;

import model.BooleanModel;

public class AcceptDeliveryListener extends AbstractListener implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	private JList deliveryOrdersList;
	
	public AcceptDeliveryListener(Observable obs, JList jl){
		addModel(obs);
		
		deliveryOrdersList = jl;
	}
	
	@Override
	public void actionPerformed(ActionEvent evt){
		BooleanModel model = (BooleanModel)getModel(0);
		model.setValue(false);
		
		Document dom = Dom.getXMLFile("bookOfDelivery.xml");
		Element root = dom.getRootElement();
		
		Element delivery = root.getChild("delivery"+(deliveryOrdersList.getSelectedIndex()+1));
				
		Element gone = delivery.getChild("gone");
		gone.setText("yes");
		
		Element date = delivery.getChild("date");
		date.setText(Date.getDate());
		
		Element time = delivery.getChild("time");
		time.setText(Time.getTime());
		
		Dom.saveFile(dom, "bookOfDelivery.xml");
	}
}
